package com.avic.rong.userOrder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.userOrder.event.InformUpdate;
import com.avic.rong.userOrder.bean.HistoryLookBean;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XinEr on 2019/7/16.
 * Describe:
 */
public class HistoryLookListAdapter extends RecyclerView.Adapter<HistoryLookListAdapter.ViewHolder> {
    private Context context;
    private ArrayList<HistoryLookBean> listData;
    public int mSelectedPos = -1;//保存当前选中的position
    private  int taiTouID;
    //单选
    private int selectPostion=-3;
    public HistoryLookListAdapter(Context context, ArrayList<HistoryLookBean> listData, int taiTouID) {
        this.context = context;
        this.listData = listData;
        this.taiTouID=taiTouID;
    }

    public int getSelectPostion() {
        return selectPostion;
    }

    public void setSelectPostion(int selectPostion) {
        this.selectPostion = selectPostion;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.history_look_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        HistoryLookBean historyLookBean = listData.get(position);
        if (!TextUtils.isEmpty(historyLookBean.getInvPayee())&&!historyLookBean.getInvPayee().equals("null")){
            holder.tv_title.setText(historyLookBean.getInvPayee() + "");
        }
        if (!TextUtils.isEmpty(historyLookBean.getCompanyAddress())&&!historyLookBean.getCompanyAddress().equals("null")){
            holder.tv_phone.setText("地址.电话:" + historyLookBean.getCompanyAddress() + "");
        }
        if (!TextUtils.isEmpty(historyLookBean.getBlank())&&!historyLookBean.getBlank().equals("null")){
            holder.tv_open_bank.setText("开户行及账号:" + historyLookBean.getBlank() + "");
        }
        if (!TextUtils.isEmpty(historyLookBean.getInvPayeeNumber())&&!historyLookBean.getInvPayeeNumber().equals("null")){
            holder.tv_shuihao.setText("税号:" + historyLookBean.getInvPayeeNumber() + "");
        }


        if (taiTouID!=-1&&taiTouID==historyLookBean.getPayeeId()){
            holder.history_select_btn.setSelected(true);
        }

        if (selectPostion == position && selectPostion != -3) {
            holder.history_select_btn.setSelected(true);
        } else if (selectPostion != -3) {
            holder.history_select_btn.setSelected(false);
        }
        holder.history_select_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setSelectPostion(position);
                notifyDataSetChanged();


                    InformUpdate informUpdate = new InformUpdate();
                    informUpdate.setEventType(informUpdate.HISTORY_FAPIAO_INFORM);
                    informUpdate.setHistoryLookBean(listData.get(position));
                    EventBus.getDefault().post(informUpdate);

            }
        });
        holder.setIsRecyclable(false);
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView history_select_btn;
        private TextView tv_title;
        private TextView tv_shuihao;
        private TextView tv_phone;
        private TextView tv_open_bank;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            history_select_btn = itemView.findViewById(R.id.history_select_btn);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_shuihao = itemView.findViewById(R.id.tv_shuihao);
            tv_phone = itemView.findViewById(R.id.tv_phone);
            tv_open_bank = itemView.findViewById(R.id.tv_open_bank);
        }
    }
}
